package com.cheyun.netsalev3.view

import android.view.View
import android.view.animation.AlphaAnimation
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.RecyclerView
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyPagedListAdapter
import com.cheyun.netsalev3.bean.Data
import com.cheyun.netsalev3.databinding.ActivityPassengerReceptionBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.utils.CommonFunc
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.utils.api.NetworkState
import com.cheyun.netsalev3.viewmodel.PassengerReceptionActivityViewModel

class PassengerReceptionActivity : BaseActivity<ActivityPassengerReceptionBinding>() {
    override fun initContentView(): Int {
        return R.layout.activity_passenger_reception
    }

    override fun initViewModel(): ViewModel? {
        //默认取前七天数据（包括当天）
        var stime = CommonFunc.getPastDate(7)
        if(intent.hasExtra("currentDay")){
            if(intent.getBooleanExtra("currentDay",false)){
                stime = CommonFunc.getCurrentDate();
            }
        }
        var etime = CommonFunc.getCurrentDate();
        var factory = InjectorUtils.getPassengerReceptionViewModelFactory(stime,etime)
        return ViewModelProviders.of(this, factory).get(PassengerReceptionActivityViewModel::class.java)
    }

    lateinit var adpter: MyPagedListAdapter<Data>
    override fun initView() {
        adpter = MyPagedListAdapter(R.layout.passenger_reception_item, binding.viewModel)
        var recyclerView = binding.root.findViewById<RecyclerView>(R.id.swipe_target)
        recyclerView.adapter = adpter
        binding.swipeRefreshLayout.setOnRefreshListener {
            binding.viewModel?.refresh()
        }
    }

    override fun initData() {
        binding.viewModel?.listdata?.observe(this, Observer { lists ->
            adpter.submitList(lists)
        })
        binding.viewModel?.networkState?.observe(this, Observer {
            binding.swipeRefreshLayout.isRefreshing = it == NetworkState.LOADING
            adpter.setNetworkState(it)
        })
        binding.viewModel?.total?.observe(this, Observer { it ->
            binding.totleText.text = "共" + it.toString() + "条数据"
            showTips()
        })
    }

    private fun showTips(){
        binding.totleText.setVisibility(View.VISIBLE)
//        binding.totleText.clearAnimation()
        val animation = AlphaAnimation(1f, 0f)
        animation.duration = 3000
        binding.totleText.startAnimation(animation)
        binding.totleText.setVisibility(View.GONE)
    }


}
